package com.lanju.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanju.common.SpringUtils;
import com.lanju.entity.Money;
import com.lanju.entity.SearchLog;
import com.lanju.entity.SharePrize;
import com.lanju.entity.User;
import com.lanju.service.JedisService;
import com.lanju.service.SharePrizeService;
import com.lanju.service.UserService;

@Controller
@RequestMapping("sharePrize")
public class SharePrizeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(SharePrizeController.class);

	@Autowired
	SharePrizeService sharePrizeService;
	
	 @Resource
		JedisService jedisService;

	@Autowired
	UserService userService;
	/**
	 * 是否有领取奖品
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping("hadShareLog")
	@ResponseBody
	public Map<String, Object> hadShareLog(HttpServletRequest request,HttpSession session) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		User user =(User) session.getAttribute("userModel");
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User u = userService.getByPhone(param);
		if (u == null) {//尚未注册
			resultMap.put("msg", "noregister");
			return resultMap;
		} else {
			List<SharePrize> sharelist = sharePrizeService.hassharePrize(param.get("phone").toString());//是否领奖
			if(sharelist !=null && sharelist.size()>0){
				resultMap.put("msg", "hadprize");
				return resultMap;
			}else{
				List<SearchLog> shareloglist= sharePrizeService.hasShareLog(param.get("phone").toString());
				if(shareloglist!= null && shareloglist.size()>0){
					resultMap.put("msg","shared");
				}else{
					resultMap.put("msg","needtoshare");
				}
			}
		}
		return resultMap;
	}
	/**
	 * 判断是否已经领奖
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping("isgetPrize")
	@ResponseBody
	public Map<String, Object> isgetPrize(HttpServletRequest request) {
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<SharePrize> sharelist = sharePrizeService.hassharePrize(param.get("phone").toString());
		if (sharelist != null && sharelist.size() > 0) {// 已经领取
			resultMap.put("msg", "已经领取");
			return resultMap;
		}else{
			
		}
		return null;
	}
	/**
	 * 领奖
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping("inserSharePrize")
	@ResponseBody
	public Map<String,Object> inserSharePrize(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		return sharePrizeService.insertSharePrize(param);
	}
	
	@SuppressWarnings("unused")
	@RequestMapping("inserShareLog")
	@ResponseBody
	public Map<String,Object> inserShareLog(HttpServletRequest request){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		return sharePrizeService.inserShareLog(param);
	}
	
	@RequestMapping("getMoneyByPhone")
	@ResponseBody
	public Map<String,Object> getMoneyByPhone(HttpServletRequest request,HttpSession session){
		Map<String, Object> param = SpringUtils.getParameterMap(request);
		Map<String, Object> resultMap = new HashMap<String, Object>();
		User user =(User) session.getAttribute("userModel");
		if(user!=null){
			param.put("phone", user.getPhone());
		}
		Money m=sharePrizeService.getMoneyByPhone(param);
		resultMap.put("money", m);
		return resultMap;
	}
}
